<template>
  <!-- 主体区域 -->
  <section id="app">
    <BlackHeader @add="add"></BlackHeader>
    <BlackMain :list="list" @send="del"></BlackMain>
    <BlackFooter
      v-show="list.length > 0"
      :list="list"
      @clear="clear"
    ></BlackFooter>
  </section>
</template>

<script>
import BlackHeader from "./components/BlackHeader";
import BlackMain from "./components/BlackMain";
import BlackFooter from "./components/BlackFooter";
export default {
  components: {
    BlackHeader,
    BlackMain,
    BlackFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, todo: "睡觉" },
        { id: 2, todo: "干饭" },
        { id: 3, todo: "喝快乐水" },
      ],
    };
  },
  methods: {
    del(id) {
      this.list = this.list.filter((item) => item.id != id);
    },
    clear() {
      this.list = [];
    },
    add(value) {
      console.log(value);
      this.list.push({
        id: +new Date(),
        todo: value,
      });
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
};
</script>

<style>
</style>